package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import com.pay.ioopos.DataFactory;
import com.pay.ioopos.DataStore;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PayMode;
import com.pay.ioopos.R;
import com.pay.ioopos.fragment.ModeAmountFragment;
import com.pay.ioopos.fragment.ModeAmountFragment.OnAmountListner;
import com.pay.ioopos.fragment.PwdCheckerFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.PayMode.FIXED;
import static com.pay.ioopos.PayMode.NORMAL;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;

public class ModeActivity extends AbstractActivity implements OnAmountListner {
    private PayMode mode;
    private String amount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mode);
        Intent intent = getIntent();
        int i = intent.getIntExtra("mode", 0);
        mode = PayMode.values()[i];
        setMainFragment(new ModeAmountFragment(mode, this));

    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        if (mainFragment instanceof ModeAmountFragment) {

        } else if (mainFragment instanceof PwdCheckerFragment) {
            PwdCheckerFragment fragment = ((PwdCheckerFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            if (keyInfo == KEY_CANCEL) {
                onCancel();
            }
        } else if (mainFragment instanceof TipVerticalFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                onCancel();
            }
        } else {
            if (keyInfo == KEY_CANCEL) {
                onCancel();
            }
        }
        return false;
    }

    @Override
    public void onFinish(String amount) {
        this.amount = amount;
        PwdCheckerFragment fragment = new PwdCheckerFragment();
        fragment.setOnPwdValidateListener(createPwdValidateListener());
        setMainFragment(fragment);
    }

    @Override
    public void onCancel() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

    private PwdCheckerFragment.OnPwdValidateListener createPwdValidateListener() {
        return new PwdCheckerFragment.OnPwdValidateListener() {

            @Override
            public void onValid(String pwd) {
                DataStore store = DataFactory.getDataStore();
                if (mode == NORMAL) {
                    store.setMaxAmount(amount);

                } else if (mode == FIXED) {
                    store.setFixAmount(amount);
                }
                store.setMode(mode);
                setMainFragment(new TipVerticalFragment(SUCCESS, "已切换至" + mode.getText()));
            }

            @Override
            public void onInvalid(String pwd) {
            }
        };
    }
}
